package com.ds.dsfans.magicapi.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ds.dsfans.common.annotation.Log;
import com.ds.dsfans.common.enums.BusinessType;
import com.ds.dsfans.magicapi.domain.MagicApiFile;
import com.ds.dsfans.magicapi.service.IMagicApiFileService;
import com.ds.dsfans.common.core.controller.BaseController;
import com.ds.dsfans.common.core.domain.AjaxResult;
import com.ds.dsfans.common.utils.poi.ExcelUtil;
import com.ds.dsfans.common.core.page.TableDataInfo;

/**
 * 脚本列表Controller
 * 
 * @author dsfans
 * @date 2024-07-11
 */
@Controller
@RequestMapping("/magicapi/file")
public class MagicApiFileController extends BaseController
{
    private String prefix = "magicapi/file";

    @Autowired
    private IMagicApiFileService magicApiFileService;

    @RequiresPermissions("magicapi:file:view")
    @GetMapping()
    public String file()
    {
        return prefix + "/file";
    }

    /**
     * 查询脚本列表列表
     */
    @RequiresPermissions("magicapi:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MagicApiFile magicApiFile)
    {
        startPage();
        List<MagicApiFile> list = magicApiFileService.selectMagicApiFileList(magicApiFile);
        return getDataTable(list);
    }

    /**
     * 导出脚本列表列表
     */
    @RequiresPermissions("magicapi:file:export")
    @Log(title = "脚本列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MagicApiFile magicApiFile)
    {
        List<MagicApiFile> list = magicApiFileService.selectMagicApiFileList(magicApiFile);
        ExcelUtil<MagicApiFile> util = new ExcelUtil<MagicApiFile>(MagicApiFile.class);
        return util.exportExcel(list, "脚本列表数据");
    }

    /**
     * 新增脚本列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存脚本列表
     */
    @RequiresPermissions("magicapi:file:add")
    @Log(title = "脚本列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MagicApiFile magicApiFile)
    {
        return toAjax(magicApiFileService.insertMagicApiFile(magicApiFile));
    }

    /**
     * 修改脚本列表
     */
    @RequiresPermissions("magicapi:file:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        MagicApiFile magicApiFile = magicApiFileService.selectMagicApiFileById(id);
        mmap.put("magicApiFile", magicApiFile);
        return prefix + "/edit";
    }

    /**
     * 修改保存脚本列表
     */
    @RequiresPermissions("magicapi:file:edit")
    @Log(title = "脚本列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MagicApiFile magicApiFile)
    {
        return toAjax(magicApiFileService.updateMagicApiFile(magicApiFile));
    }

    /**
     * 删除脚本列表
     */
    @RequiresPermissions("magicapi:file:remove")
    @Log(title = "脚本列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(magicApiFileService.deleteMagicApiFileByIds(ids));
    }
}
